﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using MsgPack.Serialization;
using DeviceCommon;
using System.IO;
using System.Diagnostics;

namespace TestClient
{
    public class KClient
    {
        public Socket socketClient { get; set; }
        public int timeInterval = 5000;
        public void Main(string[] args)
        {
            //创建实例
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint point = new IPEndPoint(ip, 3666);
            //进行连接
            socketClient.Connect(point);


            //不停的接收服务器端发送的消息
            Thread thread = new Thread(Recive);
            thread.IsBackground = true;
            thread.Start();

            StartHeartBeatThread();

            string s = Console.ReadLine();
            if (s.ToUpper() == "S")
            {
                Console.WriteLine("暂停发送心跳包");
                timeInterval = 30 * 1000;
            }

            Console.ReadKey();

        }

        //发送心跳包
        public void StartHeartBeatThread()
        {
            Byte[] buffter;
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    buffter = Encoding.UTF8.GetBytes("6002:1" + "\r\n");
                    //buffter = Encoding.UTF8.GetBytes("asdasd");
                    socketClient.Send(buffter);
                    Console.WriteLine("向服务器发送了一个心跳包");
                    Thread.Sleep(timeInterval);
                }
            });
        }
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="o"></param>
        public void Recive()
        {
            
            while (true)
            {
                //获取发送过来的消息
                byte[] buffer = new byte[1024 * 1024 * 2];
                var effective = socketClient.Receive(buffer);
                if (effective == 0)
                {
                    continue;
                }
                //var str = Encoding.UTF8.GetString(buffer, 0, effective);           
                ResponseMsgPackge pack = MsgPackHandler.ConvertToObject<ResponseMsgPackge>(buffer, 0, effective);
                Trace.WriteLine("来自服务器---" + pack.Result);
                Trace.WriteLine("来自服务器---" + pack.Success);
                Trace.WriteLine("来自服务器---" + pack["Value"]);
                Trace.WriteLine("来自服务器---" + pack.DeviceName);
                Thread.Sleep(1000);
            }
        }


        public void Send()
        {
            int i = 0;
            while (true)
            {
                i++;
                var buffter = Encoding.UTF8.GetBytes("ClientSend " + i + "\r\n");
                var temp = socketClient.Send(buffter);
                //Console.WriteLine(i);
                Thread.Sleep(1000);
            }

        }
    }
}
